﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using BLL;

namespace IAATO
{
    public partial class CategorieManagement : System.Web.UI.Page
    {
        private CategorieService categorieService;

        protected void Page_Load(object sender, EventArgs e)
        {
            categorieService = new CategorieService(new CategorieManager());

            #region binding gridView

            List<CATEGORIE> categories = categorieService.getAllCategories();
            if (categories == null)
            {
                lblError.Text = "An error has occurred";
            }
            else if (categories.Count == 0)
            {
                lblError.Text = "No category in the database";
                gdwCategories.DataBind();
            }
            else
            {
                lblError.Text = "";
                gdwCategories.DataSource = categories;
                gdwCategories.DataBind();
            }

            #endregion
        }

        protected void gdwCategories_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }

        protected void btnAjouter_Click(object sender, EventArgs e)
        {
            categorieService.createCategorie(txtNom.Text, int.Parse(txtCap.Text));

            Response.Redirect("CategorieManagement.aspx");
        }

        protected void btnModifier_Click(object sender, EventArgs e)
        {
            categorieService.updateCategorie(int.Parse(idUpdate.Value), txtNom.Text, int.Parse(txtCap.Text));

            btnModifier.Visible = false;
            btnAnnuler.Visible = false;
            btnAjouter.Visible = true;

            Response.Redirect("CategorieManagement.aspx");
        }

        protected void btnAnnuler_Click(object sender, EventArgs e)
        {
            btnModifier.Visible = false;
            btnAnnuler.Visible = false;
            btnAjouter.Visible = true;
            txtNom.Text = "";
        }

        protected void delete_Click(object sender, EventArgs e)
        {
            categorieService.deleteCategorie(int.Parse(((ImageButton)sender).CommandArgument));

            Response.Redirect("CategorieManagement.aspx");
        }

        protected void update_Click(object sender, EventArgs e)
        {
            CATEGORIE categorie = categorieService.getCategorieById(int.Parse(((ImageButton)sender).CommandArgument));

            txtNom.Text = categorie.NOM;
            btnAjouter.Visible = false;
            btnModifier.Visible = true;
            btnAnnuler.Visible = true;

            idUpdate.Value = "" + categorie.ID;

            hdfScrollIntoView.Value = "True";
        }

        protected void gdwCategories_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gdwCategories.DataSource = categorieService.getAllCategories();
            gdwCategories.PageIndex = e.NewPageIndex;
            gdwCategories.DataBind(); 
        }
    }
}